package cn.edu.hstc.wm.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import cn.edu.hstc.wm.R;

/**
 * @ClassName: SlideMenuListAdapter
 * @Description: 用于Slidemenu的菜单列表的适配器
 * 
 * @ModificationHistory
 */
public class SlideMenuListAdapter extends BaseAdapter {

	/*
	 * //使用SimpleAdapter String[] titles = mResources
	 * .getStringArray(R.array.slidemenu_list_title_type); // 5个 int[] icons = {
	 * R.drawable.icon_sad, R.drawable.icon_sad, R.drawable.icon_sad,
	 * R.drawable.icon_sad, R.drawable.icon_sad };
	 * 
	 * String[] from = { "icon", "title" }; int[] to = {
	 * R.id.iv_menu_list_line_icon, R.id.tv_menu_list_line_title };
	 * 
	 * List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	 * for (int i = 0; i < titles.length; i++) { Map<String, Object> map = new
	 * HashMap<String, Object>(); map.put("title", titles[i]); map.put("icon",
	 * icons[i]); list.add(map); }
	 * 
	 * SimpleAdapter adapter = new SimpleAdapter(this, list,
	 * R.layout.slidingmenu_menu_list_line, from, to) { public View getView(int
	 * position, View convertView, ViewGroup parent) { View view =
	 * super.getView(position, convertView, parent); if (position == mTag) {
	 * view.setBackgroundResource(R.drawable.bg_behind_list); // 当前位置颜色
	 * menuList.setTag(view); } else {
	 * view.setBackgroundColor(Color.TRANSPARENT); } return view; } };
	 */
	/**
	 * 使用一个ViewHolder来提高绘图效率,减少冗余的重复绘图
	 */
	private class ViewHolder {
		ImageView icon;
		TextView title;
	}

	private Context mContext;
	private String[] mItemTitle;
	private int[] icons = { R.drawable.menu_icon_main, R.drawable.menu_icon_order_list, R.drawable.menu_icon_gift, R.drawable.menu_icon_setting,
			R.drawable.menu_icon_about };// 5个
	private ViewHolder viewHolder;

	public SlideMenuListAdapter(Context context) {
		super();
		this.mContext = context;
		mItemTitle = mContext.getResources().getStringArray(R.array.slidemenu_list_title_type); // 5个
	}

	@Override
	public int getCount() {
		return mItemTitle.length;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 使用一个ViewHolder来提高绘图效率,减少冗余的重复绘图
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.slidingmenu_menu_list_item, null);
			viewHolder = new ViewHolder();
			viewHolder.icon = (ImageView) convertView.findViewById(R.id.iv_menu_list_line_icon);
			viewHolder.title = (TextView) convertView.findViewById(R.id.tv_menu_list_line_title);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.icon.setImageResource(icons[position]);
		viewHolder.title.setText(mItemTitle[position]);

		if (position == 0) { // 第一个默认选中
			convertView.setBackgroundResource(R.drawable.bg_behind_list);
		}

		// LogUtil.v("高度和宽度", "height=" + convertView.getHeight() + " width="
		// + convertView.getWidth());
		return convertView;
	}

}
